package com.am.servlet;

import com.am.bean.User;
import com.am.service.UserService;
import com.utils.Meg;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserServlet {

    @Autowired
    private UserService userService;

    @RequestMapping("/userView")
    public String userView(){
        return "user/user";
    }
    @RequestMapping("/userEditView")
    public String userEditView(){
        return "user/userPage";
    }

    @RequestMapping("/userAddView")
    public String userAddView(){
        return "user/userAdd";
    }


    @ResponseBody
    @RequestMapping("/userDate")
    public Meg userData(@Param("page") String page,@Param("limit") String limit){
        return Meg.success().add("data",userService.selectAll(limit,page));
    }

    @ResponseBody
    @RequestMapping("/userInsert")
    public Meg userInsert(User user){
        int i = userService.insert(user);
        if (i>0)
            return Meg.success();
        else
            return  Meg.file();
    }

    @ResponseBody
    @RequestMapping("/userUpdate")
    public Meg userUpdate(User user){
        int i = userService.update(user);
        if (i>0)
            return Meg.success();
        else
            return  Meg.file();
    }

    @ResponseBody
    @RequestMapping("/userDelete")
    public Meg userDelete(@Param("id") Integer id){
        int i = userService.delete(id);
        if (i>0)
            return Meg.success();
        else
            return  Meg.file();
    }
    @ResponseBody
    @RequestMapping("/userActivate")
    public Meg menuAction(@Param("id")Integer id,@Param("userActivate")String userActivate){
        userService.Action(id,userActivate);

        return Meg.success();
    }
}
